package com.lzx.iteam.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.widget.Toast;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 10月 25日
 * Time: 下午5:47
 *
 * @QQ : 1234567890
 * **********************************
 */
public class QrCodeUtil {

    private static final int QR_WIDTH = 360;
    private static final int QR_HEIGHT = 360;

    /**
     * 生成二维码
     *
     * @param qrcode
     */
    public static Bitmap createQrcode(String qrcode, Context mContext) {
        try {
            QRCodeWriter writer = new QRCodeWriter();
            if (qrcode == null || "".equals(qrcode) || qrcode.length() < 1) {
                Toast.makeText(mContext,"数据为空",Toast.LENGTH_SHORT).show();
                return null;
            }
            // TODO 编码格式
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            // TODO 二维码宽高、颜色
            BitMatrix bitMatrix = writer.encode(qrcode,
                  BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }

                }
            }

            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,
                  Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            return  bitmap;
        } catch (Exception e) {
            Log.e("All_Exception", e.toString());
            Toast.makeText(mContext,"数据太大，请重新选择",Toast.LENGTH_SHORT).show();
        }
        return null;
    }

    public static String enQrCodeOneContact(ArrayList<String> nameCard){

        StringBuilder ss = new StringBuilder();
        ss.append(String.format("BEGIN:VCARD\n" +
              "VERSION:3.0\n" +
              "N:%s",nameCard.get(0)))
              .append(String.format("\nTEL;iPhone;VOICE:%s",nameCard.get(1)))
              .append("\nORG:" + nameCard.get(3))
              .append("\nTITLE:"+nameCard.get(4))
              .append("\nEMAIL:"+nameCard.get(5))
              .append("\nADR:"+nameCard.get(6))
              .append("\nEND:VCARD");
        ;
        return ss.toString();

    }

    /**
     * 生成多号码二维码字符串
     * @param map
     * @return
     */
    public static String enQrCodeString(Map<String,Object> map){
        String name = (String) map.get("name");
        ArrayList<Map<String,String>> contact = (ArrayList<Map<String,String>>) map.get("phones");
        ArrayList<String> phones = new ArrayList<String>();
        for (int j = 0;j<contact.size();j++){
            Map<String,String> phone = contact.get(j);
            phones.add(phone.get("number"));
        }
        String qrcode = "";
        if (phones != null && phones.size() > 0){
            StringBuilder ss = new StringBuilder();
            String head = String.format("BEGIN:VCARD\n" +
                  "VERSION:3.0\n" +
                  "N:%s",name);
            ss.append(head);
            for (int j = 0;j< phones.size();j++){
                String tel = String.format("\nTEL;iPhone;VOICE:%s",phones.get(j));
                ss.append(tel);
            }
            if (map.containsKey("email")){
                ss.append("\nEMAIL:"+map.get("email"));
            }
            if (map.containsKey("address")){
                ss.append("\nADR:"+map.get("address"));
            }
            if (map.containsKey("company")){
                ss.append("\nORG:"+map.get("company"));
            }
            if (map.containsKey("title")){
                ss.append("\nTITLE:"+map.get("title"));
            }
            if (map.containsKey("note")){
                ss.append("\nNOTE:"+map.get("note"));
            }
            ss.append("\nEND:VCARD");
            qrcode = ss.toString();
        }
        Log.d("enQrCodeString","enQrCodeString == "+qrcode);
        return qrcode;
    }
}
